<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务id" prop="taskId">
      <el-input v-model="dataForm.taskId" placeholder="任务id"></el-input>
    </el-form-item>
    <el-form-item label="活动id" prop="marketName">
      <el-input v-model="dataForm.marketName" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="当前步骤" prop="currentStep">
      <el-input v-model="dataForm.currentStep" placeholder="当前步骤"></el-input>
    </el-form-item>
    <el-form-item label="当前步骤进度" prop="currentProcess">
      <el-input v-model="dataForm.currentProcess" placeholder="当前步骤进度"></el-input>
    </el-form-item>
    <el-form-item label="最后一次更新时间" prop="lastUpdate">
      <el-input v-model="dataForm.lastUpdate" placeholder="最后一次更新时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="排序字段，越大越靠前" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段，越大越靠前"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          taskId: '',
          marketName: '',
          userId: '',
          state: '',
          currentStep: '',
          currentProcess: '',
          lastUpdate: '',
          createTime: '',
          updateTime: '',
          sort: '',
          status: '',
          isDelete: ''
        },
        dataRule: {
          taskId: [
            { required: true, message: '任务id不能为空', trigger: 'blur' }
          ],
          marketName: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          currentStep: [
            { required: true, message: '当前步骤不能为空', trigger: 'blur' }
          ],
          currentProcess: [
            { required: true, message: '当前步骤进度不能为空', trigger: 'blur' }
          ],
          lastUpdate: [
            { required: true, message: '最后一次更新时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段，越大越靠前不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/usertaskrecord/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.taskId = data.userTaskRecord.taskId
                this.dataForm.marketName = data.userTaskRecord.marketName
                this.dataForm.userId = data.userTaskRecord.userId
                this.dataForm.state = data.userTaskRecord.state
                this.dataForm.currentStep = data.userTaskRecord.currentStep
                this.dataForm.currentProcess = data.userTaskRecord.currentProcess
                this.dataForm.lastUpdate = data.userTaskRecord.lastUpdate
                this.dataForm.createTime = data.userTaskRecord.createTime
                this.dataForm.updateTime = data.userTaskRecord.updateTime
                this.dataForm.sort = data.userTaskRecord.sort
                this.dataForm.status = data.userTaskRecord.status
                this.dataForm.isDelete = data.userTaskRecord.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/usertaskrecord/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'taskId': this.dataForm.taskId,
                'marketName': this.dataForm.marketName,
                'userId': this.dataForm.userId,
                'state': this.dataForm.state,
                'currentStep': this.dataForm.currentStep,
                'currentProcess': this.dataForm.currentProcess,
                'lastUpdate': this.dataForm.lastUpdate,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
